﻿Imports System
Imports System.IO

Public Class Log
    Public Shared Sub writeLog(ByVal strMsg As String)
        Dim logFile As String = ""
        Try
            logFile = System.IO.Path.Combine(My.Settings.PathFiles, "Logs\")
            If Not Directory.Exists(logFile) Then
                Directory.CreateDirectory(logFile)
            End If
            logFile = Path.Combine(logFile, String.Format("log{0:yyyyMMdd}.txt", DateTime.Now))

            Using sw As StreamWriter = New StreamWriter(logFile, True)
                sw.WriteLine((DateTime.Now + (" - " + strMsg)))
            End Using
        Catch
            'throw;
        Finally
            logFile = Nothing
        End Try
    End Sub

    Public Shared Function getPathFiles() As String
        Return System.IO.Path.Combine(My.Settings.PathFiles, "Logs\")
    End Function

    Public Shared Sub LogException(ByVal exc As Exception, ByVal source As String)
        Dim logFile As String = ""
        Try
            logFile = System.IO.Path.Combine(My.Settings.PathFiles, "Logs\")
            If Not Directory.Exists(logFile) Then
                Directory.CreateDirectory(logFile)
            End If
            logFile = Path.Combine(logFile, String.Format("log{0:yyyyMMdd}.txt", DateTime.Now))
            ' Open the log file for append and write the log
            Using sw As StreamWriter = New StreamWriter(logFile, True)
                sw.WriteLine("********** {0} **********", DateTime.Now)
                If ((exc.InnerException) IsNot Nothing) Then
                    sw.Write("Inner Exception Type: ")
                    sw.WriteLine(exc.InnerException.GetType.ToString)
                    sw.Write("Inner Exception: ")
                    sw.WriteLine(exc.InnerException.Message)
                    sw.Write("Inner Source: ")
                    sw.WriteLine(exc.InnerException.Source)
                    If ((exc.InnerException.StackTrace) IsNot Nothing) Then
                        sw.WriteLine("Inner Stack Trace: ")
                        sw.WriteLine(exc.InnerException.StackTrace)
                    End If
                End If
                sw.Write("Exception Type: ")
                sw.WriteLine(exc.GetType.ToString)
                sw.WriteLine(("Exception: " + exc.Message))
                sw.WriteLine(("Source: " + source))
                sw.WriteLine("Stack Trace: ")
                If ((exc.StackTrace) IsNot Nothing) Then
                    sw.WriteLine(exc.StackTrace)
                    sw.WriteLine()
                End If
                sw.Close()
            End Using
        Catch

        Finally
            logFile = Nothing
        End Try
    End Sub
End Class
